package com.sinodata.bsm.cicp.collector.jboss;

import java.util.ArrayList;
import java.util.List;

import javax.management.ObjectName;

import org.apache.log4j.Logger;
import org.jboss.jmx.adaptor.rmi.RMIAdaptor;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.RmiUtil;
import com.sinodata.bsm.common.constants.DataTypeConstants;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * <p>
 * Description:  Jboss事务管理器各项指标的采集
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-7-24 下午09:50:44          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class JTACollector extends Collector {
    private static Logger logger = Logger.getLogger(JTACollector.class);

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
        boolean statisticsEnabled = true;
        try {
            //            RMIAdaptor rmiAdapter = RmiUtil.getRmiAdaptor(ip, Integer.parseInt(this.getParamMap().get("端口")));
            RMIAdaptor rmiAdapter = RmiUtil.getRmiAdaptor(ip, Integer.parseInt(this.getParamMap().get("Port")));
            ObjectName jbossTransactinoManager = new ObjectName("jboss:service=TransactionManager");

            statisticsEnabled = (Boolean) rmiAdapter.getAttribute(jbossTransactinoManager, "StatisticsEnabled");
            Long transactionCountValue = (Long) rmiAdapter.getAttribute(jbossTransactinoManager, "TransactionCount");
            Long rollbackCountValue = (Long) rmiAdapter.getAttribute(jbossTransactinoManager, "RollbackCount");
            String transactionCount = transactionCountValue + "";
            String commitCount = rmiAdapter.getAttribute(jbossTransactinoManager, "CommitCount") + "";
            String rollbackCount = rollbackCountValue + "";
            String rollbackRatio = ((transactionCountValue > 0) ? (rollbackCountValue * 100 / transactionCountValue) : 0) + "";
            // propValues[0] = ResPropertyValue.createPropValue(statisticsEnabled ? "YES" : "NO", propMetas[0]);
            propValues[0] = ResPropertyValue.createPropValue(statisticsEnabled ? DataTypeConstants.BOOLEAN_VALUE_TRUE : DataTypeConstants.BOOLEAN_VALUE_FALSE, propMetas[0]);
            propValues[1] = ResPropertyValue.createPropValue(transactionCount, propMetas[1]);
            propValues[2] = ResPropertyValue.createPropValue(commitCount, propMetas[2]);
            propValues[3] = ResPropertyValue.createPropValue(rollbackCount, propMetas[3]);
            propValues[4] = ResPropertyValue.createPropValue(rollbackRatio, propMetas[4]);

            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
            return values;
        } catch (Exception e) {
            logger.error(e);
            throw new CollectorException(e);
        }
    }

    @Override
    public String[] getParamMeta() {
        //        return new String[] { "端口" };
        return new String[] { "Port" };
    }

    @Override
    public String[] getPropMeta() {
        //        return new String[] { "统计是否启用", "事务数量", "提交事务数", "回滚事务数", "事务回滚率" };
        //        return new String[] { "StatWhetherStarted", "TransactionsNum", "SubmitTransactionsNum", "RollbackTransactionsNum", "TransactionRollbackRate" };
        return new String[] { "$1", "$2", "$3", "$4", "$5" };
    }

}
